<?php
/**
 * Spiffy Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is
 * available through the world-wide-web at this URL:
 * http://www.spiffyjr.me/license
 *
 * @category   	Spiffy
 * @package    	Spiffy_Db
 * @copyright  	Copyright (c) 2009 Kyle Spraggs. http://www.spiffyjr.me
 * @license 	http://www.spiffyjr.me/license	New BSD License
 * @author 		Kyle Spraggs <theman@spiffyjr.me>
 */

/**
 * Class to provide a custom table object that utilizes Spiffy_Db_Table_Select,
 * a joinMap for automated SQL generation using joins, and automatic table
 * naming based on the last word of the class name (Default_Model_Event maps
 * to a table named 'event').
 *
 * This class assumes the following about your table structure:
 * 		- All tables/columns use lowercase naming with no CamelCase.
 * 			e.g., user_id instead of UserId.
 *
 * @category	Spiffy
 * @package		Spiffy_Db
 * @copyright  	Copyright (c) 2009 Kyle Spraggs. http://www.spiffyjr.me
 * @license 	http://www.spiffyjr.me/license	New BSD License
 */
class Spiffy_Db_Table extends Zend_Db_Table
{
	protected $_select = null;
	
	/**
	 * Cache of table names to ensure no class instance is
	 * loaded more than once.
	 * @var array
	 */
	protected $_referenceCache = array();
	
	/**
	 * Returns an instance of a Spiffy_Db_Table_Select object.
	 *
	 * @param bool $withFromPart Whether or not to include the from part of the select based on the table
	 * @return Zend_Db_Table_Select
	 */
	public function get($cols = array('*'))
	{
		if (null === $this->_select) {
			require_once 'Spiffy/Db/Table/Select.php';
			$this->_select = new Spiffy_Db_Table_Select($this);
			$this->_select->setIntegrityCheck(false)->from($this->_name, $cols);
		}
		
		return $this->_select;
	}
	
	/**
	 * Queries the database using the prepared select.
	 * @param bool $reset If true resets the in process select (default true) 
	 * @return array
	 */
	public function query($reset = true)
	{
		$resultSet = $this->fetchAll($this->get());
		
		if ($reset) {
			$this->_select = null;
		}
		
		return $resultSet;
	}
	
	/**
	 * Queries the database using the prepared select for
	 * a single row.
	 * @param bool $reset If true resets the in process select (default true)
	 * @return array
	 */
	public function queryOne($reset = true)
	{
		$row = $this->fetchRow($this->get());
		
		if ($reset) {
			$this->_select = null;
		}
		return $row;
	}
	
	/**
	 * Gets a table name based on the class. Results are cached
	 * in _referenceCache to prevent loading multiple instances
	 * of the same cache.
	 *
	 * @param string $tableClass
	 * @return string
	 */
	public function getRefTableName($tableClass)
	{
		if (!isset($this->_referenceCache[$tableClass])) {
			if (!class_exists($tableClass)) {
				throw new Spiffy_Db_Table_Exception(
					'Table class \'' . $tableClass . '\' could not be loaded.
					Does it exist?');
			}
			$table = new $tableClass();
			
			if (!$table instanceof Zend_Db_Table_Abstract) {
				throw new Spiffy_Db_Table_Exception(
					$tableClass . ' exists but is not an instance of Zend_Db_Table_Abstract');
			}
			
			$this->_referenceCache[$tableClass] = $table->info(Zend_Db_Table::NAME);
		}
		return $this->_referenceCache[$tableClass];
	}

}
